/*
 * A CORBA Generic Server
 * Part of the GICOM project
 */
package gicom.config;

import java.io.File;
import java.util.Properties;
import org.omg.CORBA.Policy;
import org.omg.PortableServer.IdAssignmentPolicyValue;
import org.omg.PortableServer.LifespanPolicyValue;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAPackage.WrongPolicy;
import org.omg.PortableServer.RequestProcessingPolicyValue;
import org.omg.PortableServer.ServantRetentionPolicyValue;

/**
 * provides the configuration of a POA
 * @author Guillaume Missonnier
 */
public class POAConfig extends GenericConfig {
    
    /**
     * The POA's ORB's id
     */
    private String idORB;
    
    /**
     * The POA's idAssignment
     */
    private String idAssignment;
    
    /**
     * The POA's requestProcessing
     */
    private String requestProcessing;
    
    /**
     * The POA's servantRetention
     */
    private String servantRetention;
    
    /**
     * The POA's lifeSpan
     */
    private String lifeSpan;
    
    public POAConfig (String id) throws Exception {
        super("poa_", id);
        this.load();
    }
    
    protected void doSave () throws Exception {
        // Build the properties
        Properties properties = new Properties();
        properties.setProperty("idORB",getIdORB());
        properties.setProperty("idAssignment", getIdAssignment().toString());
        properties.setProperty("requestProcessing", getRequestProcessing().toString());
        properties.setProperty("servantRetention", getServantRetention().toString());
        properties.setProperty("lifeSpan", getLifeSpan().toString());
        // Save the properties
        saveProperties(properties);
    }
    
    protected void doLoad () throws Exception {
        Properties properties = loadProperties();
        idORB = properties.getProperty("idORB");
        if (getIdORB() == null) throw new Exception("idORB is not define.");
        idAssignment = properties.getProperty("idAssignment");
        if (getIdAssignment() == null) throw new Exception("IdAssignment is not define.");
        requestProcessing = properties.getProperty("requestProcessing");
        if (getRequestProcessing() == null) throw new Exception("RequestProcessing is not define for POA.");
        servantRetention = properties.getProperty("servantRetention");
        if (getServantRetention() == null) throw new Exception("ServantRetention is not define.");
        lifeSpan = properties.getProperty("lifeSpan");
        if (getLifeSpan() == null) throw new Exception("LifeSpan is not define.");
    }
    
    /**
     * Build the policies object to instanciate the POA
     * @return The POA's 
     */
    public Policy[] getPolicies (POA poa) throws WrongPolicy {
        Policy[] policies = new Policy[4];
        IdAssignmentPolicyValue idAssignmentPolicyValue;
        if (getIdAssignment().equals("SYSTEM_ID")) idAssignmentPolicyValue = IdAssignmentPolicyValue.SYSTEM_ID;
        else if (getIdAssignment().equals("USER_ID")) idAssignmentPolicyValue = IdAssignmentPolicyValue.USER_ID;
        else throw new WrongPolicy("Wrong idAssignment.");
        policies[0] = poa.create_id_assignment_policy(idAssignmentPolicyValue);
        RequestProcessingPolicyValue requestProcessingPolicyValue;
        if (getRequestProcessing().equals("USE_ACTIVE_OBJECT_MAP_ONLY")) requestProcessingPolicyValue = RequestProcessingPolicyValue.USE_ACTIVE_OBJECT_MAP_ONLY;
        else if (getRequestProcessing().equals("USE_DEFAULT_SERVANT")) requestProcessingPolicyValue = RequestProcessingPolicyValue.USE_DEFAULT_SERVANT;
        else if (getRequestProcessing().equals("USE_SERVANT_MANAGER")) requestProcessingPolicyValue = RequestProcessingPolicyValue.USE_SERVANT_MANAGER;
        else throw new WrongPolicy("Wrong requestProcessing.");
        policies[1] = poa.create_request_processing_policy(requestProcessingPolicyValue);
        ServantRetentionPolicyValue servantRetentionPolicyValue;
        if (getServantRetention().equals("NON_RETAIN")) servantRetentionPolicyValue = ServantRetentionPolicyValue.NON_RETAIN;
        else if (getServantRetention().equals("RETAIN")) servantRetentionPolicyValue = ServantRetentionPolicyValue.RETAIN;
        else throw new WrongPolicy("Wrong servantRetention.");
        policies[2] = poa.create_servant_retention_policy(servantRetentionPolicyValue);
        LifespanPolicyValue lifespanPolicyValue;
        if (getLifeSpan().equals("PERSISTENT")) lifespanPolicyValue = LifespanPolicyValue.PERSISTENT;
        else if (getLifeSpan().equals("TRANSIENT")) lifespanPolicyValue = LifespanPolicyValue.TRANSIENT;
        else throw new WrongPolicy("Wrong lifeSpan.");
        policies[3] = poa.create_lifespan_policy(lifespanPolicyValue);
        return policies;
    }

    public String getIdORB() {
        return idORB;
    }

    public String getIdAssignment() {
        return idAssignment;
    }

    public String getRequestProcessing() {
        return requestProcessing;
    }

    public String getServantRetention() {
        return servantRetention;
    }

    public String getLifeSpan() {
        return lifeSpan;
    }

}
